package com.zzxz.practicaltrainingsys.controller;

import com.zzxz.practicaltrainingsys.entity.Teacher;
import com.zzxz.practicaltrainingsys.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

@Api(tags = "教师接口端")
@RestController
@RequestMapping("/teacher")
@CrossOrigin(origins = "http://localhost:5173")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    // 教师列表
    // http://localhost:8080/teacher/list
    @GetMapping("/list")
    public List<Teacher> getTeacherList() {
        return teacherService.getTeacher();
    }

    // 分页查询教师信息
    // http://localhost:8080/teacher/pages?pageNum=1&pageSize=10
    @GetMapping("/pages")
    public Map<String, Object> getTeacherPages(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        List<Teacher> all = teacherService.getTeacher();
        int total = all.size();
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);
        List<Teacher> pageList = new ArrayList<>();
        if (fromIndex < total) {
            pageList = all.subList(fromIndex, toIndex);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageList);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return result;
    }

    @ApiOperation("根据教师id更新教师密码")
    @PutMapping("/updatePassword/{teacher_id}")
    public boolean updatePassword(
            @PathVariable Integer teacher_id,
            @RequestBody Map<String, String> request) {
        String password = request.get("teacher_password");
        return teacherService.updatePassword(teacher_id, password);
    }

    // 教师信息（全部）
    // http://localhost:8080/teacher/updateAll/{teacher_id}
    @ApiOperation("根据教师ID更新教师信息")
    @PostMapping("/updateAll/{teacher_id}")
    public ResponseEntity<String> updateAll(
            @PathVariable Integer teacher_id,
            @RequestBody Teacher teacher) {
        teacher.setTeacher_id(teacher_id);
        boolean success = teacherService.updateTeacher(teacher);
        return success ?
                ResponseEntity.ok("教师信息更新成功") :
                ResponseEntity.badRequest().body("教师信息更新失败");
    }

    // 根据老师输入的账号查取密码
    // http://localhost:8080/teacher/findTeacherPassword/{teacher_id}
    @ApiOperation("根据老师输入的账号查取密码")
    @GetMapping("/findTeacherPassword/{teacher_id}")
    public ResponseEntity<Map<String, String>> getTeacherPassword(
            @PathVariable("teacher_id") Integer teacherId) {
        String password = teacherService.getTeacherPassword(teacherId);
        if (password != null) {
            Map<String, String> response = new HashMap<>();
            response.put("password", password);
            return ResponseEntity.ok(response);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 新增教师
    @PostMapping("/insert")
    public int insertTeacher(@RequestBody Teacher teacher) {
        // 这里简单实现，实际应校验工号唯一性等
        List<Teacher> all = teacherService.getTeacher();
        if (teacher.getTeacher_id() == null || teacher.getTeacher_id().toString().trim().isEmpty()) {
            return -1; // 工号不能为空
        }
        for (Teacher t : all) {
            if (t.getTeacher_id().equals(teacher.getTeacher_id())) {
                return 0; // 工号已存在
            }
        }
        boolean success = teacherService.insertTeacher(teacher);
        return success ? 1 : -2; // 1成功，-2失败
    }

    // 修改删除单个教师接口
    @DeleteMapping("/delete")
    public int deleteTeacher(@RequestParam Integer teacher_id) {
        boolean success = teacherService.deleteTeacher(teacher_id);
        return success ? 1 : 0;
    }

    // 修改批量删除教师接口
    @PostMapping("/deleteBatch")
    public int deleteBatch(@RequestBody List<Integer> ids) {
        boolean success = teacherService.deleteBatch(ids);
        return success ? 1 : 0;
    }

    // 查询单个教师详情
    @GetMapping("/view/{teacher_id}")
    public Teacher viewTeacher(@PathVariable Integer teacher_id) {
        return teacherService.getTeacherById(teacher_id);
    }

    // 教师id登录
    @GetMapping("/teaLoginById/{teacher_id}")
    public Teacher loginTeacher(@PathVariable Integer teacher_id) {
        return teacherService.loginTeacherById(teacher_id);
    }

    // 教师电话登录
    @GetMapping("/teaLoginByPhone/{teacher_phone}")
    public Teacher loginTeacherByPhone(@PathVariable String teacher_phone) {
        return teacherService.loginTeacherByPhone(teacher_phone);
    }
}